/* ssc install _gwtmean // Needed for wtmean calculations */

// Table 3: Regression and R-squared estimates

local PATH_TBL = "~/Downloads/tables"
local PATH_NUM = "~/Downloads/numbers"


capture program drop wide_to_grouped_long 
program wide_to_grouped_long
    args dtafn stub1 stub2 stub3
    use `dtafn', clear
    capture drop item_id_2
    gen item_id_2 = _n
    keep item_id_2 `stub1'_choice_* `stub2'_choice_* `stub3'_choice_* spending yes_is_lib
    reshape long `stub1'_choice_@ `stub2'_choice_@ `stub3'_choice_@, i(item_id_2 yes_is_lib spending) j(pid D_pid R_pid S_pid) string
    rename *_ *
    rename *_choice n*
    gen pid_num = 1 if pid ==  "D_pid" 
    replace pid_num = 0 if pid == "S_pid"
    replace pid_num = -1 if pid == "R_pid"
    drop pid
    reshape long n@, i(item_id_2 yes_is_lib spending pid_num) j(choice) string

    // Additional recodes/selections 
    drop if pid_num == .
    drop if pid_num == 0
    replace pid_num = 0 if pid_num == -1

    gen byte `stub1'_choice = 1 if choice == "`stub1'"
    gen byte `stub2'_choice = 1 if choice == "`stub2'"
    gen byte `stub3'_choice = 1 if choice == "`stub3'"
    gen byte X_choice = 1 if choice != "`stub1'" & choice != "`stub2'" & choice != "`stub3'"
end

capture program drop center_vars 
program center_vars
    args stub
    egen mean_`stub'_choice = wtmean(`stub'_choice), weight(n) by(item_id_2)
    egen mean_pid_num = wtmean(pid_num), weight(n) by(item_id_2)
    replace `stub'_choice = `stub'_choice - mean_`stub'_choice
    replace pid_num = pid_num - mean_pid_num
end


/* STATE PARTISAN OFFICES */
wide_to_grouped_long wide_state_partisan_races D R U

egen xD = sum(D_choice*n), by(item_id_2)
egen xR = sum(R_choice*n), by(item_id_2)
drop if (xD/(xD + xR) < .05 | xD/(xD + xR) > .95)
replace D_choice = 0 if R_choice == 1
drop if D_choice == .
drop R_choice
/* 
*  xtreg can't handle our weights and areg can't (?) produce a within R2
*  so, we mean diff to fit the fe regression by hand
*/ 


center_vars D
reg D_choice pid_num [fw=n], vce(cluster item_id_2) hascons
local b_1 =  _b[pid_num]
local s_1 = _se[pid_num]
local r_1 = e(r2)
local n_1 = e(N)


/* LOCAL PARTISAN OFFICES */
wide_to_grouped_long wide_local_partisan_races D R U

egen xD = sum(D_choice*n), by(item_id_2)
egen xR = sum(R_choice*n), by(item_id_2)
drop if (xD/(xD + xR) < .05 | xD/(xD + xR) > .95)
replace D_choice = 0 if R_choice == 1
drop if D_choice == .
drop R_choice

center_vars D
reg D_choice pid_num [fw=n], vce(cluster item_id_2) hascons
local b_2 =  _b[pid_num]
local s_2 = _se[pid_num]
local r_2 = e(r2)
local n_2 = e(N)


/* STATE NON-PARTISAN OFFICES */
wide_to_grouped_long wide_state_nonpartisan_races A B U

egen xA = sum(A_choice*n), by(item_id_2)
egen xB = sum(B_choice*n), by(item_id_2)
drop if (xA/(xA + xB) < .05 | xA/(xA + xB) > .95)
drop xA xB
replace A_choice = 0 if B_choice == 1
drop if A_choice == .
drop B_choice

egen x = wtmean(A_choice) if pid_num == 1, by(item_id_2) weight(n)
egen A_D = max(x), by(item_id_2)
drop x
egen x = wtmean(A_choice) if pid_num == 0, by(item_id_2) weight(n)
egen A_R = max(x), by(item_id_2)
drop x
drop if A_D == . | A_R == .
gen byte D_choice = A_choice     if A_D >= A_R
replace  D_choice = 1 - A_choice if A_D <  A_R

center_vars D
reg D_choice pid_num [fw=n], vce(cluster item_id_2) hascons
local b_3 =  _b[pid_num]
local s_3 = _se[pid_num]
local r_3 = e(r2)
local n_3 = e(N)


/* LOCAL NON-PARTISAN OFFICES */
wide_to_grouped_long wide_local_nonpartisan_races A B U

egen xA = sum(A_choice*n), by(item_id_2)
egen xB = sum(B_choice*n), by(item_id_2)
drop if (xA/(xA + xB) < .05 | xA/(xA + xB) > .95)
drop xA xB
replace A_choice = 0 if B_choice == 1
drop if A_choice == .
drop B_choice

egen x = wtmean(A_choice) if pid_num == 1, by(item_id_2) weight(n)
egen A_D = max(x), by(item_id_2)
drop x
egen x = wtmean(A_choice) if pid_num == 0, by(item_id_2) weight(n)
egen A_R = max(x), by(item_id_2)
drop x
drop if A_D == . | A_R == .
gen byte D_choice = A_choice     if A_D >= A_R
replace  D_choice = 1 - A_choice if A_D <  A_R

center_vars D
reg D_choice pid_num [fw=n], vce(cluster item_id_2) hascons
local b_4 =  _b[pid_num]
local s_4 = _se[pid_num]
local r_4 = e(r2)
local n_4 = e(N)


/* STATE BALLOT MEASURES */
wide_to_grouped_long wide_state_ballot_measures Y N U

replace Y_choice = 0 if N_choice == 1
drop if Y_choice == .
drop N_choice
egen x = wtmean(Y_choice) if pid_num == 1, by(item_id_2) weight(n)
egen Y_D = max(x), by(item_id_2)
drop x
egen x = wtmean(Y_choice) if pid_num == 0, by(item_id_2) weight(n)
egen Y_R = max(x), by(item_id_2)
drop x
drop if Y_D == . | Y_R == .
gen byte L_choice = Y_choice     if Y_D >= Y_R
replace  L_choice = 1 - Y_choice if Y_D <  Y_R

center_vars L
reg L_choice pid_num [fw=n], vce(cluster item_id_2) hascons
local b_5 =  _b[pid_num]
local s_5 = _se[pid_num]
local r_5 = e(r2)
local n_5 = e(N)

// manual coding of L
drop L_choice
gen byte L_choice = Y_choice if yes_is_lib == 1
replace  L_choice = 1 - Y_choice if yes_is_lib == 0
drop if L_choice == .

// Center new L_choice var by hand as pid is already centered!!
drop mean_L_choice
egen mean_L_choice = wtmean(L_choice), weight(n) by(item_id_2)
replace L_choice = L_choice - mean_L_choice

reg L_choice pid_num [fw=n], vce(cluster item_id_2) hascons
local b_7 = _b[pid_num]
local s_7 = _se[pid_num]
local r_7 = e(r2)
local n_7 = e(N)

// reg L_choice pid_num [fw=n] if spending == 1, vce(cluster item_id_2)
// local b_8 =  _b[pid_num]
// local s_8 = _se[pid_num]
// local r_8 = e(r2)
// local n_8 = e(N)

/* LOCAL BALLOT MEASURES */
wide_to_grouped_long wide_local_ballot_measures Y N U

replace Y_choice = 0 if N_choice == 1
drop if Y_choice == .
drop N_choice

egen x = wtmean(Y_choice) if pid_num == 1, by(item_id_2) weight(n)
egen Y_D = max(x), by(item_id_2)
drop x
egen x = wtmean(Y_choice) if pid_num == 0, by(item_id_2) weight(n)
egen Y_R = max(x), by(item_id_2)
drop x
drop if Y_D == . | Y_R == .
gen byte L_choice = Y_choice     if Y_D >= Y_R
replace  L_choice = 1 - Y_choice if Y_D <  Y_R

center_vars L
reg L_choice pid_num [fw=n], vce(cluster item_id_2)
local b_6 =  _b[pid_num]
local s_6 = _se[pid_num]
local r_6 = e(r2)
local n_6 = e(N)

// manual coding of L
drop L_choice
gen byte L_choice = Y_choice if yes_is_lib == 1
replace  L_choice = 1 - Y_choice if yes_is_lib == 0
drop if L_choice == .

// Center new L_choice var by hand as pid is already centered!!
drop mean_L_choice
egen mean_L_choice = wtmean(L_choice), weight(n) by(item_id_2)
replace L_choice = L_choice - mean_L_choice

reg L_choice pid_num [fw=n], vce(cluster item_id_2) hascons
local b_9 =  _b[pid_num]
local s_9 = _se[pid_num]
local r_9 = e(r2)
local n_9 = e(N)

// reg L_choice pid_num [fw=n] if spending == 1, vce(cluster item_id_2) hascons
// local b_10 =  _b[pid_num]
// local s_10 = _se[pid_num]
// local r_10 = e(r2)
// local n_10 = e(N)


quietly {
    capture log close
    log using "`PATH_TBL'/table_3.tex", replace text
    noisily display "\begin{table}[htpb] "
    noisily display "\centering \small"
    noisily display "\begin{threeparttable} "
    noisily display "\caption{\bf Voting Patterns by Partisanship, Regression Estimates} "
    noisily display "\label{table_3} "
    noisily display "\begin{tabular}{l r r r r} "
    noisily display "\toprule\toprule "
    noisily display "          &           & Within    &       \\ "
    noisily display "Outcome   & Democrat  & R-square  & Obs.  \\ "
    noisily display "\midrule "
    noisily display "State Partisan Offices      &  " %5.3f `b_1' "  &  " %5.3f `r_1' " & " %12.0fc `n_1' " \\ "
    noisily display "                            & (" %5.3f `s_1' ") &  "             " & "               " \\ [.02in] "
    noisily display "Local Partisan Offices      &  " %5.3f `b_2' "  &  " %5.3f `r_2' " & " %12.0fc `n_2' " \\ "
    noisily display "                            & (" %5.3f `s_2' ") &  "             " & "               " \\ [.02in] "
    noisily display "State Non-Partisan Offices  &  " %5.3f `b_3' "  &  " %5.3f `r_3' " & " %12.0fc `n_3' " \\ "
    noisily display "                            & (" %5.3f `s_3' ") &  "             " & "               " \\ [.02in] "
    noisily display "Local Non-Partisan Offices  &  " %5.3f `b_4' "  &  " %5.3f `r_4' " & " %12.0fc `n_4' " \\ "
    noisily display "                            & (" %5.3f `s_4' ") &  "             " & "               " \\ [.02in] "
    noisily display "State Ballot Measures       &  " %5.3f `b_5' "  &  " %5.3f `r_5' " & " %12.0fc `n_5' " \\ "
    noisily display "                            & (" %5.3f `s_5' ") &  "             " & "               " \\ [.02in] "
    noisily display "Local Ballot Measures       &  " %5.3f `b_6' "  &  " %5.3f `r_6' " & " %12.0fc `n_6' " \\ "
    noisily display "                            & (" %5.3f `s_6' ") &  "             " & "               " \\ [.02in] "
    noisily display "\bottomrule "
    noisily display "\end{tabular}"
    noisily display "\begin{tablenotes} "
    noisily display "\footnotesize \item Each row is a separate regression. Standard errors, clustered by contests, are in parentheses. "
    noisily display "Fixed effects for contests included in all cases.  "
    noisily display "\end{tablenotes} "
    noisily display "\end{threeparttable} "
    noisily display "\end{table} "
    log close
}


quietly {
  capture log close
  log using "`PATH_NUM'/rsq_handcode_it_sbm.tex", replace text
  noisily display %5.3f `r_7'
  log close
}

quietly {
  capture log close
  log using "`PATH_NUM'/rsq_handcode_it_sbm_N.tex", replace text
  noisily display %13.0gc `n_7'
  log close
}

quietly {
  capture log close
  log using "`PATH_NUM'/rsq_handcode_it_lbm.tex", replace text
  noisily display %5.3f `r_9'
  log close
}

quietly {
  capture log close
  log using "`PATH_NUM'/rsq_handcode_it_lbm_N.tex", replace text
  noisily display %13.0gc `n_9'
  log close
}
